package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Exam;
import com.alpha.bom.Professor;
import com.alpha.dao.ExamDao;
import com.alpha.dao.QuestionDao;


/**
 * Servlet implementation class ExamsAdminServlet
 */
@WebServlet("/ExamsAdminServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class ExamsAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(ExamsAdminServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExamsAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		Professor professor = null;
		HttpSession session = request.getSession();
		String category = "all";
		String is_real = "all";
		String message=null;
		String error = null;
		String success = null;
		
		if(request.getParameter("success")!=null){
			success = request.getParameter("success");
		} else if(request.getParameter("error")!=null){
			error = request.getParameter("error");
		} else if(request.getParameter("message")!=null){
			message = request.getParameter("message");
		}
		
		if(session.getAttribute("user")!=null){
			professor = (Professor) session.getAttribute("user");
			
			if(request.getParameter("categories")!=null&&!request.getParameter("categories").equals("")){
				category = request.getParameter("categories");
			}
			
			if(request.getParameter("exam_type")!=null&&!request.getParameter("exam_type").equals("")){
				is_real = request.getParameter("exam_type");
			}
			
			ExamDao eDao = new ExamDao();
			
			if(is_real.equalsIgnoreCase("all")){
				if(category.equalsIgnoreCase("all")){
					try {
						professor.setExams(eDao.retrieveExamByProfessorId(professor.getUserId()));
					} catch (NamingException e) {
						logger.error(e.getMessage());
						return;
					} catch (SQLException e) {
						logger.error(e.getMessage());
						return;
					}
				}else{
					try {
						professor.setExams(eDao.retrieveExamByCategory(professor.getUserId(), category));
					} catch (NamingException e) {
						logger.error(e.getMessage());
						return;
					} catch (SQLException e) {
						logger.error(e.getMessage());
						return;
					}
				}
			}else{
				boolean real = is_real.equalsIgnoreCase("1")?true:false;
				if(category.equalsIgnoreCase("all")){
					try {
						professor.setExams(eDao.retrieveExamByProfessorId(professor.getUserId(), real));
					} catch (NamingException e) {
						logger.error(e.getMessage());
						return;
					} catch (SQLException e) {
						logger.error(e.getMessage());
						return;
					}
				}else{
					try {
						professor.setExams(eDao.retrieveExamByCategory(professor.getUserId(), category, real));
					} catch (NamingException e) {
						logger.error(e.getMessage());
						return;
					} catch (SQLException e) {
						logger.error(e.getMessage());
						return;
					}
				}
			}
			
			for(Exam exam:professor.getExams()){
				QuestionDao qDao = new QuestionDao();
				try {
					exam.setQuestions(qDao.retrieveQuestions(exam.getExamId()));
				} catch (NamingException e) {
					logger.error(e.getMessage());
					return;
				} catch (SQLException e) {
					logger.error(e.getMessage());
					return;
				}
			}
			if(error!=null){
				response.sendRedirect("ExamsAdmin.jsp?error="+error);
			} else if(success!=null){
				response.sendRedirect("ExamsAdmin.jsp?success="+success);
			} else if(message!=null){
				response.sendRedirect("ExamsAdmin.jsp?message="+message);
			} else {
				response.sendRedirect("ExamsAdmin.jsp");
			}
		}else{
			response.sendRedirect("Login.jsp");
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
